package org.misty.scala.first

import java.io.{File, FileOutputStream, PrintWriter}
import javafx.application.Application
import javafx.scene.Scene
import javafx.scene.control.Label
import javafx.scene.layout.StackPane
import javafx.stage.Stage

import scalafx.Includes._
import scalafx.beans.property.StringProperty

/**
 * Created by Misty on 2015/6/9.
 */
class App extends Application {
    val text = new StringProperty("Loading")

    def use(writer: AutoCloseable): (=> Unit) => Any = {
        (block) => {
            try {
                block
            } finally {
                writer.close()
            }
        }
    }

    override def start(primaryStage: Stage): Unit = {
        val label = new Label()
        label.text <== text

        val scene = new Scene(new StackPane(label), 400, 300)
        primaryStage.setTitle("Scala JavaFX Application")
        primaryStage.setScene(scene)
        primaryStage.show()
        App.end = System.currentTimeMillis()

        val total = App.end - App.start
        println(total)

        val file = new File("scala.txt")
        if (file.exists()) {
            val os = new PrintWriter(new FileOutputStream(file, true))
            use(os) {
                os.append(total + "\n")
            }
        }
        text.value = "Loaded"
    }
}

object App {
    var start = 0L
    var end = 0L

    def main(args: Array[String]) {
        start = System.currentTimeMillis()
        Application.launch(classOf[App], args: _*)
    }
}
